package org.snail.department.service.impl;

import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.StringUtils;
import org.snail.constant.ErrCodeConstant;
import org.snail.dept.mapper.DepartmentMapper;
import org.snail.exception.EmpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Log4j
public class DepartmentServiceImpl {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 通过部门id查找部门下员工
     *
     * @param deptId
     * @return
     */
    public List<String> findUserIdListByDeptId(Long deptId) throws EmpException {
        List<String> userIdListFromDB = new ArrayList<String>();
        if (deptId==null || deptId<1) {
            throw new EmpException(ErrCodeConstant.ERROR_CODE_NO_EMPID, "没有员工id");
        } else {
            userIdListFromDB = departmentMapper.findUserIdListByDeptId(deptId);
        }
        return userIdListFromDB;
    }

    /**
     * 通过userId查询部门id
     * @param userId    员工userId
     * @return
     * @throws EmpException
     */
    public int findDeptIdByUserId(String userId) throws EmpException {
        int result = 0;
        if(StringUtils.isEmpty(userId)){
            throw new EmpException(ErrCodeConstant.ERROR_CODE_NO_EMPID,"没有userId");
        }
        try{
            result =   departmentMapper.findDeptIdByUserId(userId);
        }catch (Exception e){
            log.error("根据userid查询部门id失败:"+e.getMessage());
        }

        return result;
    }
    /**
     * 保存部门和人员关系
     * @param id
     * @param addUseridList
     * @return
     * @throws EmpException
     */
    public boolean saveDeptUserList(Long id, List<String> addUseridList) throws EmpException {
        boolean result = true;
        if (null==id || id<1 || null==addUseridList || addUseridList.isEmpty()) {
            result = false;
            log.error("部门ID有误，或者人员列表为空" + ", id: " + id + ", addUseridList: " + addUseridList);
        } else {
            departmentMapper.saveDeptUserList(id, addUseridList);
        }
        return result;
    }

    /**
     * 删除部门和人员关系
     * @param id
     * @param delUseridList
     * @return
     * @throws EmpException
     */
    public boolean deletDeptUserList(Long id, List<String> delUseridList) throws EmpException {
        boolean result = true;
        if (null==id || id<1 || null==delUseridList || delUseridList.isEmpty()) {
            result = false;
            log.error("部门ID有误，或者人员列表为空" + ", id: " + id + ", delUseridList: " + delUseridList);
        } else {
            departmentMapper.deletDeptUserList(id, delUseridList, new Date());
        }
        return result;
    }

    /**
     * 检查人员是否在公司存在
     * @param userId
     * @return
     */
    public List<String> checkUserIdInDepartmentUserList(String userId) {
        return departmentMapper.checkUserIdInDepartmentUserList(userId);
    }


}
